import XDialog from "@/x-dialog/index";

/**
 * 弹窗
 */
export class Dialog {
  static _title = "温馨提示";
  static _width = "300px";
  static _confirmColor = "#fa6400";

  /**
   * 单个按钮的提示弹窗
   * @param text 提示文本
   * @param callback 点击之后的回调
   */
  static alert(text: string, callback?: () => void) {
    // #ifdef MP
    const dialog = new XDialog({
      title: Dialog._title,
      message: text,
      onConfirm: callback,
    });
    dialog.show();
    // #endif

    // #ifdef WEB
    uni.showModal({
      title: Dialog._title,
      content: text,
      width: Dialog._width,
      confirmText: "知道了",
      confirmColor: Dialog._confirmColor,
      showCancel: false,
      success: (r: { confirm: any; }) => {
        if (r.confirm) {
          if (callback) {
            callback();
          }
        }
      },
    });
    // #endif
  }

  /**
   * 双按钮的提示弹窗
   * @param text 提示文本
   * @param confirmText 确认按钮文本
   * @param cancelText 取消按钮文本
   * @param confirmCallback 点击确认按钮之后的回调
   * @param cancelCallback 点击取消按钮之后的回调
   */
  static confirm(opt: {
    text: string;
    cancelLabel?: string;
    confirmLabel?: string;
    confirmCallback?: () => void;
    cancelCallback?: () => void;
  }) {
    // #ifdef MP
    const dialog = new XDialog({
      title: Dialog._title,
      message: opt.text,
      confirmText: opt.confirmLabel,
      cancelText: opt.cancelLabel,
      onConfirm: opt.confirmCallback,
      onCancel: opt.cancelCallback,
    });
    dialog.show();
    // #endif

    // #ifdef WEB
    uni.showModal({
      title: Dialog._title,
      content: opt.text,
      width: Dialog._width,
      confirmText: opt.confirmLabel || "确定",
      cancelText: opt.cancelLabel || "取消",
      confirmColor: Dialog._confirmColor,
      success: (r: { confirm: any; cancel: any; }) => {
        if (r.confirm) {
          if (opt.confirmCallback) {
            opt.confirmCallback();
          }
        } else if (r.cancel) {
          if (opt.cancelCallback) {
            opt.cancelCallback();
          }
        }
      },
    });
    // #endif
  }

  /**
   * 轻提示
   */
  static toast(text: string) {
    uni.showToast({
      title: text,
      icon: "none",
    });
  }

  /**
   * 显示加载弹窗
   */
  static showLoading() {
    uni.showLoading({
      title: "加载中...",
      mask: true,
    });
  }

  /**
   * 关闭加载弹窗
   */
  static closeLoading() {
    uni.hideLoading();
  }
}
